package main

import (
	"fmt"
	"sort"
)

func main() {
	//testMake1()
	//testMake2()
	//test3()
	//test4()
	//emptySecion()
	//testAppend()
	//test8()
	//testCopy()
	//test9()
	testSort()
}

func testMake1() {
	i := make([]int, 1, 2) //第一个 参数是长度，第二个参数是 需扩容的长度的限定范围
	i[0] = 100
	fmt.Printf("i = %v addr = %p\n", i, i) //i = [100] addr = 0xc000016070
	i = append(i, 200)
	fmt.Printf("i = %v addr = %p\n", i, i) //i = [100 200] addr = 0xc000016070
	i = append(i, 300)
	fmt.Printf("i = %v addr = %p\n", i, i) //i = [100 200] addr = 0xc00004a040
}

func testMake2() {
	a := make([]int, 0, 10)                                     //不限定 切片的长度
	fmt.Printf("a = %v,len = %v,cap = %v\n", a, len(a), cap(a)) //a = [],len = 0,cap = 10
	a = append(a, 10)
	fmt.Printf("a = %v,len = %v,cap = %v\n", a, len(a), cap(a)) //a = [10],len = 1,cap = 10
}

//对比容量 与 长度
func test3() {
	a := []string{"1", "2", "3", "4", "5", "6", "7"}
	fmt.Printf("a = %v,len = %v,cap = %v\n", a, len(a), cap(a)) //a = [1 2 3 4 5 6 7],len = 7,cap = 7

	b := a[1:3]
	fmt.Printf("b = %v,len = %v,cap = %v\n", b, len(b), cap(b)) //b = [2 3],len = 2,cap = 6
}

//切片在切片
func test4() {
	a := []string{"1", "2", "3", "4", "5", "6", "7"}
	fmt.Printf("a = %v,len = %v,cap = %v\n", a, len(a), cap(a)) //a = [1 2 3 4 5 6 7],len = 7,cap = 7

	b := a[1:3]
	fmt.Printf("b = %v,len = %v,cap = %v\n", b, len(b), cap(b)) //b = [2 3],len = 2,cap = 6

	c := b[:len(b)-1]                                           //切片的切片
	fmt.Printf("c = %v,len = %v,cap = %v\n", c, len(c), cap(c)) //[2 3 4 5 6],len = 5,cap = 6
}

//空切片
func emptySecion() {
	var a = []int{}
	a = append(a, 100)
	fmt.Printf("a = %v,len = %v,cap = %v\n", a, len(a), cap(a)) //a = [100],len = 1,cap = 1

	a = append(a, 200)
	fmt.Printf("a = %v,len = %v,cap = %v\n", a, len(a), cap(a)) //a = [100],len = 1,cap = 2

	a = append(a, 300)
	fmt.Printf("a = %v,len = %v,cap = %v\n", a, len(a), cap(a)) //a = [100],len = 1,cap = 4
}

//切片追加切片
func testAppend() {
	var a []int = []int{1, 2, 3}
	var b []int = []int{4, 5, 6}

	a = append(a, b...)
	fmt.Printf("a = %v\n", a)
}

func test8() {
	a := [10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	fmt.Printf("sum = %v\n", testParam(a[:]))
}

//切片的传参
func testParam(a []int) int {
	sum := 0
	for _, v := range a {
		sum += v
	}
	return sum
}

//切片的复制
func testCopy() {
	a := []int{1, 2, 3}
	b := []int{4, 5, 6}
	copy(a, b)
	fmt.Printf("a = %v\n", a) //[4 5 6]
	fmt.Printf("b = %v\n", b) //[4 5 6]

	c := []int{1, 2}
	d := []int{4, 5, 6}
	copy(c, d)
	fmt.Printf("c = %v\n", c) //[4 5]
	fmt.Printf("d = %v\n", d) //[4 5 6]
}

func test9() {
	sa := make([]string, 5, 10)
	fmt.Println(sa)
	for i := 0; i < 10; i++ {
		sa = append(sa, fmt.Sprintf("%v", i))
	}
	fmt.Println(sa)
}
func testSort() {
	b := [...]int{4, 3, 2, 1, 5, 9, 8, 7, 6}
	a := b[:]
	//sort.Sort(sort.Reverse(sort.IntSlice(a)))//倒序
	sort.Sort(sort.IntSlice(a))
	fmt.Println("After reversed: ", a) //正序
}
